/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainDACFrame.java
 *
 * Created on 2011-04-05, 10:55:45
 */
package bsk_dac;

import bsk_dac.database.DatabaseFacade;
import bsk_dac.database.DatabaseUser;
import bsk_dac.database.SelectingDatabaseUserFrame;
import bsk_dac.users.IUsersManager;
import bsk_dac.users.UsersManager;
import bsk_dac.tables.TablesManager;
import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.pushingpixels.substance.api.skin.SubstanceModerateLookAndFeel;

public class MainDACFrame extends javax.swing.JFrame {

    private static IUsersManager usersManager = null;
    private static DACPanel dacPanel = null;
    public static boolean adminMode = false;
    public static boolean isAdmin = false;
    //  public static PrivilegesMode privMode = PrivilegesMode.GIVE_PRIVILEGES;
    private TablesManager tableManager = TablesManager.getTableManager();
    public static Connection conn;
    private UserPanel selectedUserPanel;
    private NewUserFrame newUserFrame;
    private DeleteUserFrame deleteUserFrame;
    private String adminUser = "";

    public MainDACFrame() {
        try {
            UIManager.setLookAndFeel(new SubstanceModerateLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainDACFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        dacPanel = new DACPanel(this);
        dacPanel.setSize(1050, 650);
        add(dacPanel);
        initComponents();
        this.setVisible(true);
        showLoginFrame();
        newUserFrame = new NewUserFrame(this);
        newUserFrame.setVisible(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Discretionary Access Control + PP");
        setMinimumSize(new java.awt.Dimension(900, 600));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        jButton1.setText("Przełącz użytkownika");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("LEGENDA"));

        jCheckBox1.setText("Brak uprawnień");
        jCheckBox1.setEnabled(false);

        jCheckBox2.setBackground(java.awt.Color.red);
        jCheckBox2.setForeground(java.awt.Color.red);
        jCheckBox2.setSelected(true);
        jCheckBox2.setText("Uprawnienie bez przekazywania uprawnień");
        jCheckBox2.setEnabled(false);

        jCheckBox3.setSelected(true);
        jCheckBox3.setText("Uprawnienie z przekazywaniem uprawnień");
        jCheckBox3.setEnabled(false);

        jCheckBox4.setBackground(java.awt.Color.green);
        jCheckBox4.setForeground(java.awt.Color.green);
        jCheckBox4.setSelected(true);
        jCheckBox4.setText("Możliwość wykonania revoke uprawnienia");
        jCheckBox4.setEnabled(false);

        jLabel1.setText("Dla niezalogowanego użytkownika");

        jLabel2.setText("Dla zalogowanego użytkownika");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(120, 120, 120)
                                .addComponent(jCheckBox2))
                            .addComponent(jCheckBox1))
                        .addGap(3, 3, 3)
                        .addComponent(jCheckBox3))
                    .addComponent(jLabel1)
                    .addComponent(jCheckBox4)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(jLabel2)
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox2)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox3))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(15, 15, 15)
                .addComponent(jCheckBox4))
        );

        getContentPane().add(jPanel1);

        jMenu1.setText("Dodawanie");

        jMenuItem1.setText("Dodaj użytkownika");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Usuwanie");

        jMenuItem2.setText("Usuń użytkownika");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Ustal przejmowanie uprawnień");
        jMenu3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu3ActionPerformed(evt);
            }
        });

        jMenuItem3.setText("Ustal użytkowników");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(MainDACFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_formWindowClosed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (selectedUserPanel != null && selectedUserPanel.isButtonSelected() == true) {
            selectedUserPanel.toggleButton();
        }
        showLoginFrame();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        newUserFrame = new NewUserFrame(this);
        newUserFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        deleteUserFrame = new DeleteUserFrame(this);
        deleteUserFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu3ActionPerformed

    }//GEN-LAST:event_jMenu3ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        PrzejmowanieFrame przejFrame = new PrzejmowanieFrame(this);
        przejFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainDACFrame().setVisible(false);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    public static IUsersManager getUsersManager() {
        if (usersManager == null) {
            usersManager = new UsersManager(dacPanel);
        }
        return usersManager;
    }

 

    public void loadAdminMode() {
        jMenuBar1.setVisible(true);

        if (usersManager == null) {
            usersManager = new UsersManager(dacPanel);
        } else {
            dacPanel.clear();
            usersManager.loadUsers();
        }

        dacPanel.refreshPanelsAccess();
        GridLayout layout = (GridLayout) dacPanel.getLayout();
        layout.setRows(usersManager.getUsersCount() + 1);
        layout.setColumns(tableManager.getAvaiableTables().size() + 1);
        dacPanel.repaint();
        pack();
        repaint();
    }

    public void loadPrivilegesPanel() {


        if (isAdmin == true) {
            jMenuBar1.setVisible(true);
        } else {
            jMenuBar1.setVisible(false);
        }
        if (usersManager == null) {
            usersManager = new UsersManager(dacPanel);
        } else {
            dacPanel.clear();
            usersManager.loadUsers();
        }

        //Zaznaczenie zalogowanego użytkownika
        for (UserPanel userPanel : dacPanel.getUsersPanels()) {
            if (userPanel.getUser().getUserName().equals(DatabaseUser.userName)) {
                selectedUserPanel = userPanel;
                userPanel.toggleButton();
            }
        }

        dacPanel.refreshPanelsAccess();
        GridLayout layout = (GridLayout) dacPanel.getLayout();
        layout.setRows(usersManager.getUsersCount() + 1);
        layout.setColumns(tableManager.getAvaiableTables().size() + 1);
        dacPanel.repaint();
        pack();
        repaint();
    }

    public void removeUser(String userName) {
        try {

            for (UserPanel userPanel : dacPanel.getUsersPanels()) {
                if (userPanel.getUser().getUserName().equals(userName)) {
                    userPanel.removeAllPermissions();
                    DatabaseFacade.deleteUser(userName);

                }
            }

        } catch (SQLException se) {
            se.printStackTrace();
        }
    }

    public void showLoginFrame() {
        this.setVisible(false);
        SelectingDatabaseUserFrame selectingDatabaseUserFrame = new SelectingDatabaseUserFrame(this);
        selectingDatabaseUserFrame.setLocation(500, 300);
        selectingDatabaseUserFrame.setAlwaysOnTop(true);
        selectingDatabaseUserFrame.setVisible(true);
    }
}
